package com.cgsd.heyu.ui.task;

import android.os.Bundle;
import android.view.View;

import com.cgsd.heyu.R;
import com.cgsd.heyu.api.ApiService;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.http.BaseResponse;
import com.cgsd.heyu.http.BaseRxSubscribe;
import com.cgsd.heyu.http.ErrorMessage;
import com.cgsd.heyu.http.RetrofitServiceManagerPhp;
import com.cgsd.heyu.http.RxjavaHelper;
import com.cgsd.heyu.ui.BaseFragment;
import com.cgsd.heyu.ui.task.adapter.MyBookAdapter;
import com.cgsd.heyu.ui.task.vo.MyTaskVo;
import com.cgsd.heyu.utils.CommonTools;
import com.cgsd.heyu.utils.RequestParams;
import com.cgsd.heyu.widgets.refresh.NestedRefreshLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.RequestBody;

/**
 * Author: sky on 2021/3/1 17:02
 * Email: xcode126@126.com
 * Desc: 我的报名
 */
public class MyBookFragment extends BaseFragment implements
        NestedRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener {

    private MyBookAdapter bookAdapter;
    private NestedRefreshLayout mNestedRefreshLayout;
    private RecyclerView mRecyclerView;
    private int page,totalPage;

    public static MyBookFragment newInstance() {
        MyBookFragment fragment = new MyBookFragment();
        Bundle bundle = new Bundle();
//        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.common_refresh;
    }

    @Override
    protected void initView() {
        mNestedRefreshLayout = findViewById(R.id.mNestedRefreshLayout);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        bookAdapter = new MyBookAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(bookAdapter);
        mNestedRefreshLayout.setOnRefreshListener(this);
        bookAdapter.setOnLoadMoreListener(this,mRecyclerView);
        bookAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyTaskVo.TasklistBean item = bookAdapter.getItem(position);
                startActivity(TaskCommitActivity.getIntent(mContext,item.id));
            }
        });
    }

    @Override
    protected void initData() {
      onRefresh();
    }

    @Override
    public void onRefresh() {
        getTaskList(page = 1);
    }

    @Override
    public void onLoadMoreRequested() {
        if (page < totalPage) {
            getTaskList(++page);
        } else {
            bookAdapter.loadMoreEnd(true);
        }
    }

    /**
     * get Task list
     */
    public void getTaskList(int page) {
        RequestBody requestBody = new RequestParams()
                .put("page", page)
                .put("cid", 1)//分类ID,1为我的任务,2为我的发布,默认1
                .putUid()
                .get();
        RetrofitServiceManagerPhp.getInstance().creat(ApiService.class)
                .getMyTask(requestBody)
                .compose(RxjavaHelper.observeOnMainThread())
                .subscribe(new BaseRxSubscribe<BaseResponse<MyTaskVo>>() {
                    @Override
                    public void onSuccess(BaseResponse<MyTaskVo> baseResponse) {
                        processList(baseResponse.getBizBody());
                    }

                    @Override
                    public void onError(ErrorMessage error) {
                        handleErrorAction(error);
                    }
                });
    }
    /**
     * process list
     *
     * @param taskVo
     */
    private void processList(MyTaskVo taskVo) {
        if (page == 1) {
            totalPage = CommonTools.getTotalPage(taskVo.count, AppConfig.PAGE_SIZE);
            if (taskVo.count <= 0) {
                bookAdapter.getData().clear();
                bookAdapter.setEmptyView(getHelperView(mRecyclerView, R.layout.common_empty, null));
                bookAdapter.notifyDataSetChanged();
            } else {
                bookAdapter.setNewData(taskVo.tasklist);
            }
            mNestedRefreshLayout.refreshFinish();
        } else {
            bookAdapter.addData(taskVo.tasklist);
            bookAdapter.loadMoreComplete();
        }

    }
}
